define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
],function($,IO,bootbox,bootstrap,upui,up,jui){

    /**-------------数据展示----------------*/
    $(function () {
        //获取资讯类别
        IO.get("/ct/act/consultationClassification",{},function(d){
            $("#category").append('<option value="-1">请选择咨询类别</option>');
            for(var key in d.data.rows){
                $("#category").append(
                    '<option value="'+d.data.rows[key].article_cate_id+'">'+d.data.rows[key].article_cate_name+'</option>'
                )
            }

        });
        //获取培训课程
        IO.get("/trainsList",{listType:"0",page:1,rows:10},function(d){
            $("#train").append('<option value="-1">请选择培训课程</option>');
            for(var key in d.data.rows){
                $("#train").append(
                    '<option value="'+d.data.rows[key].train_id+'">'+d.data.rows[key].train_name+'</option>'
                )
            }
        });

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '资讯编号',
                dataIndex: 'article_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].article_id+"</span>";
                }
            },
            {
                title: '资讯作者',
                dataIndex: 'article_author',
                dataSource: function (data, datarow, gridobj, current_column) {
                    //if(data[datarow].article_author==undefined){
                    //    return "<span></span>";
                    //}
                   // else{
                        return "<span>"+ data[datarow].article_author+"</span>";
                    //}
                }
            },
            {
                title: '资讯标题',
                dataIndex: 'article_title',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].article_title +"</span>";
                }
            },
            {
                title: '资讯类别',
                dataIndex: 'article_cate_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].article_cate_name +"</span>";
                }
            },
            {
                title: '培训课程',
                dataIndex: 'article_cate_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].train_name +"</span>";
                }
            },
            {
                title: '创建时间',
                dataIndex: 'gender',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var date = new Date(data[datarow].timestamp);
                    create_year = date.getFullYear();
                    create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
                    create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
                    return "<span>"+create_year+'-'+create_month+'-'+create_day+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' class='editor' style='color: green;' article_id='"+data[datarow].article_id+"'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: red;' class='delete' article_id='"+data[datarow].article_id+"'>删除</a></span>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/ct/act/consultationArticle", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            var searchContent = $("#searchContent").val();
            var param = "article_title="+searchContent;
            return param;
        }
        dataInit();
        //点击查询
        $("#searchid").bind("click", function () {
            dataInit();
        });

    });
    var gb_add;
    var article_id_gb;
    /**-----------------------------------调用新增模态框-----------------------------------*/
    $("#consultationArticle").on('click',function(){
        $("#myModalLabel").text("新增资讯");
        $("#myModal").modal('show');
        gb_add = 0;
    })
    //隐藏模态框后数据清除
    $('#myModal').on('hidden.bs.modal', function () {
        $('#acticle_actor').val('');
        $('#acticle_title').val('');
        editor.html('');
        $("#category").val('-1');
        $("#train").val('-1');
        $("#upimgs").attr({src:"/images/addimg.png"});
    });
    //解决编辑器弹出层文本框不能输入的问题
    $('#myModal').off('shown.bs.modal').on('shown.bs.modal', function (e) {
        $(document).off('focusin.modal');//解决编辑器弹出层文本框不能输入的问题
    });

    /**-----------------------------------点击按钮判断模态框-----------------------------------*/
    $("#add").on('click',function(){
        var acticle_actor = $("#acticle_actor").val();//作者名字
        var acticle_title = $("#acticle_title").val();//资讯标题
        var content_true = editor.html()//资讯真实内容
        var content       = "<html><head><style>img {width: 100%;}</style></head><body>"+editor.html()+"</body></html>";//资讯内容
        var cover         = $("#upimgs").attr("src"); //资讯封面
        var category      = $("#category").val();     //资讯分类选择
        var train         = $("#train").val();        //培训课程
        //if(acticle_actor ==""){
        //    bootbox.alert("资讯作者不能为空");
        //    return;
        //}
        if(acticle_title ==""){
            bootbox.alert("资讯标题不能为空");
            return;
        }
        if(category =="-1"){
            bootbox.alert("请选择资讯类别");
            return;
        }
        if(train =="-1"){
            bootbox.alert("请选择所属培训课程");
            return;
        }
        if(cover ==""||cover == "/images/addimg.png"){
            bootbox.alert("请上传资讯封面");
            return;
        }
        if(content_true ==""){
            bootbox.alert("资讯内容不能为空");
            return;
        }

        var params={};
        params.acticle_title = acticle_title;
        params.acticle_actor = acticle_actor;
        params.content       = content;
        params.cover         = cover;
        params.category      = category;
        params.train         = train;

        //进行增加操作
        if(gb_add==0) {
            IO.post('/ct/act/acticle_add',params, function (d) {
                bootbox.alert(d.msg,function(){
                    if (d.code == 0) {
                        location.href = '/consultationArticle';
                    }
                })
            })
        }
        //进行编辑操作
        else if(gb_add==1){
            params.article_id_gb = article_id_gb;
            IO.post('/ct/act/article_edit',params, function (d) {
                bootbox.alert(d.msg,function(){
                    if (d.code == 0) {
                        location.href = '/consultationArticle';
                    }
                })
            })
        }
    })

    /**-----------------------------------获取文章详情-----------------------------------*/
    $(document).on('click','.editor',function(){
        article_id_gb=$(this).attr("article_id");
        IO.get('/ct/act/article_detail',{article_id:article_id_gb},function(d){
            $("#acticle_title").val(d.article_title);
            $("#acticle_actor").val(d.article_author);
            $("#upimgs").attr({src: d.article_cover});
            editor.html(d.article_body);
            $("#category").val(d.article_cate_id);
            $("#train").val(d.train_id);
        })
        $("#myModalLabel").text("编辑");
        $("#myModal").modal('show');
        gb_add = 1;
    })
    /**-----------------------------------删除-----------------------------------*/
    $(document).on('click','.delete',function(){
        article_id_gb = $(this).attr("article_id");
        if(article_id_gb){
            bootbox.confirm("确定要删除该文章？",function(result){
                if(result) {
                    IO.get("/ct/act/article_delete",{article_id:article_id_gb},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/consultationArticle';
                            }
                        })
                    });
                }
            });
        }
    })

    /**-------------图片上传----------------*/
    $('#fileupload').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs').src = '/images/loading.gif';
        //document.getElementById('upimgs_change').src = '/images/loading.gif';

    }).on('fileuploadprocessalways', function (e, data) {
    }).on('fileuploadprogressall', function (e, data) {
    }).on('fileuploaddone', function (e, data) {
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';
        $("#upimgs" + alt).attr("src", data._response.result.url);
        //$("#upimgs_change" + alt).attr("src", data._response.result.url);
    });
})



